/*
 * All content copyright http://www.j2eefast.com, unless
 * otherwise indicated. All rights reserved.
 * No deletion without permission
 */
package com.j2eefast.flowable.bpm.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.flowable.bpm.entity.ProjectArchivesEntity;
import com.j2eefast.flowable.bpm.service.ArchivesService;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Global;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * <p>三联管理</p>
 *
 * @author: zhangyuan
 * @date: 2021-04-29 00:02
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bpm/archive")
public class ArchiveController extends BaseController {

	private String urlPrefix = "modules/bpm/archive";

	@Autowired
	private ArchivesService archivesService;
	@Autowired
	private SysUserService sysUserService;


	//档案列表
	@RequestMapping("/archiveList")
	@RequiresPermissions("bpm:archive:archiveList")
	@ResponseBody
	public ResponseData archiveList(@RequestParam Map<String, Object> params) {
		PageUtil page = archivesService.archivesList(params);
		return success(page);
	}




	//档案列表
	@RequiresPermissions("bpm:archive:archive")
	@GetMapping("/archive")
	public String archive() {
		return urlPrefix + "/archive";
	}


	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("bpm:archive:add")
	@GetMapping("/add")
	public String add() {
		return urlPrefix + "/add";
	}


	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("bpm:archive:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated ProjectArchivesEntity projectArchivesEntity) {
		//查询用户信息
		SysUserEntity userEntity = sysUserService.getSysById(projectArchivesEntity.getNameId());
		projectArchivesEntity.setCeoName(userEntity.getName());
		//校验参数
		ValidatorUtil.validateEntity(projectArchivesEntity);
		return archivesService.save(projectArchivesEntity) ? success() : error("新增失败!");
	}

	/**
	 * 修改档案页面
	 */
	@RequiresPermissions("bpm:archive:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		ProjectArchivesEntity projectArchivesEntity = archivesService.getArchives(id);
		mmap.put("projectArchive", projectArchivesEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改认保存
	 */
	@RepeatSubmit
	@RequiresPermissions("bpm:archive:edit")
	@BussinessLog(title = "认领", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(ProjectArchivesEntity projectArchivesEntity) {
		//查询用户信息
		SysUserEntity userEntity = sysUserService.getSysById(projectArchivesEntity.getNameId());
		projectArchivesEntity.setCeoName(userEntity.getName());
		ValidatorUtil.validateEntity(projectArchivesEntity);
		return archivesService.updateByIds(projectArchivesEntity) ? success() : error("修改失败!");
	}

	/**
	 * 删除
	 */
	@RepeatSubmit
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("bpm:archive:del")
	@ResponseBody
	public ResponseData deleteArchive(HttpServletRequest request) {
		String str = request.getParameter("ids");
		if (str != "") {
			String[] s = str.split(",");
			Long[] ids = (Long[]) ConvertUtils.convert(s,Long.class);
			return archivesService.deleteArchive(ids) ? success() : error("删除失败!");
		} else {
			return this.error("删除失败");
		}

	}





	@BussinessLog(title = "项目档案导出", businessType = BusinessType.EXPORT)
	@RequiresPermissions("bpm:archive:export")
	@PostMapping("/export")
	@ResponseBody
	public ResponseData export(@RequestParam Map<String, Object> params) throws Exception {
		String fileName =  "项目档案";
		fileName = ToolUtil.encodingExcelFilename(fileName);
		String folder = Global.getTempPath() + File.separator + "pio" + File.separator;
		List<ProjectArchivesEntity> listData = archivesService.findList(params);
//内容样式策略
		WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//垂直居中,水平居中
		contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
		contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
		contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
		contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
		contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
		contentWriteCellStyle.setWrapped(true);
//头策略使用默认
		//excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
		EasyExcel.write(folder + fileName, ProjectArchivesEntity.class)
//				设置输出excel版本,不设置默认为xlsx
				.excelType(ExcelTypeEnum.XLS).head(ProjectArchivesEntity.class)
				//设置拦截器或自定义样式
				.sheet("项目档案查询")
				//设置默认样式及写入头信息开始的行数
				.useDefaultStyle(true).relativeHeadRowIndex(3)
				//这里的addsumColomn方法是个添加合计的方法,可删除
				.doWrite(listData);
		return success(fileName);
	}






}
